package com.qwer.controller.accesscontrol;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qwer.controller.comm.BaseController;
import com.qwer.model.accesscontrol.AccControlMapInfo;
import com.qwer.service.accesscontrol.AccControlMapService;
import com.qwer.service.comm.UserService;

@Controller
@RequestMapping("/accControlMap")
public class AccControlMapController extends BaseController{
    
    @Autowired
    private AccControlMapService AccControlMapService;
    
    @Autowired
    private UserService userService;
    
    
    @RequestMapping(value = { "/main"})
    public String index(Model model,HttpServletRequest req) {
        model.addAttribute("user", getLoginUser(req));
        return "page/accesscontrol/accControlMap/main";
    }
    
    
    
    @RequestMapping(value = { "/edit"})
    public String zpIndex(Integer callId,Model model,String type) {
        
        model.addAttribute("users", userService.getAllUser());
        model.addAttribute("type", type);
        model.addAttribute("callId", callId);
        return "page/accesscontrol/accControlMap/edit";
    }
    
    
    @RequestMapping("/delete")
    @ResponseBody
    public int delete(HttpServletRequest request,Integer id){
        try {
            AccControlMapService.deleteById(id);
            return 0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return -1;
    }
    
    @RequestMapping("/save")
    @ResponseBody
    public int save(HttpServletRequest request,String callUserIds,String type,Integer callId){
        try {
            AccControlMapInfo model;
            if(StringUtils.isEmpty(callUserIds)){
                return -1;
            }
            AccControlMapService.deleteByCallIdAndUserId(callId, getLoginUser(request).getId());
            for (String callUserId : callUserIds.split(",")) {
                model = new AccControlMapInfo();
                model.setUserId(getLoginUser(request).getId());
                model.setCallUserId(Integer.parseInt(callUserId));
                model.setType(type);
                model.setCallId(callId);
                AccControlMapService.save(model);
            }
            
            return 0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return -1;
    }
    
    
    @RequestMapping(value = "/getPageDatas")
    @ResponseBody
    public String getPageDatas(@RequestParam(value = "page", defaultValue = "1") Integer page,
        @RequestParam(value = "size", defaultValue = "10") Integer size,AccControlMapInfo data,HttpServletRequest req){
        Sort sort = new Sort(Direction.DESC, "id");
        data.setUserId(getLoginUser(req).getId());
        PageRequest request = new PageRequest(page - 1, size,sort);
        Page<AccControlMapInfo> zpModels = AccControlMapService.findList(request,data);
       
        
        List<AccControlMapInfo> list = zpModels.getContent();
        
        //将数据准备好layUI的格式
        JSONObject jobj = new JSONObject();
        jobj.put("code",0);
        jobj.put("msg","");
        jobj.put("count",zpModels.getTotalElements());
        jobj.put("data",JSON.parseArray(JSON.toJSONString(list)));
        System.out.println(jobj.toString());
        return jobj.toString();
    }
    
    
    @RequestMapping(value = "/getUserCheckDatas")
    @ResponseBody
    public String getUserCheckDatas(Integer callId,String type,HttpServletRequest req){
        List<AccControlMapInfo> list = AccControlMapService.getUserCheckDatas(getLoginUser(req).getId(), callId,type);
        
        JSONObject jobj = new JSONObject();
        
        jobj.put("code",0);
        jobj.put("msg","");
        jobj.put("data",JSON.parseArray(JSON.toJSONString(list)));
        System.out.println(jobj.toString());
        return jobj.toString();
    }
    
    @RequestMapping(value = "/checkPass")
    @ResponseBody
    public String checkPass(AccControlMapInfo model,HttpServletRequest req){
        model.setCallUserId(getLoginUser(req).getId());
        List<AccControlMapInfo> list = AccControlMapService.findByCallUserIdAndCallIdAndType(model);
        int code=-1;
        String msg = "没有授权，不能访问！";
        if(list.size()>0){
            msg="已被授权，可以正常访问！";
            code=0;
        }
        JSONObject jobj = new JSONObject();
        jobj.put("code",code);
        jobj.put("msg",msg);
        jobj.put("data",JSON.parseArray(JSON.toJSONString(list)));
        System.out.println(jobj.toString());
        return jobj.toString();
    }
    
    
}
